package com.scenic.rd_android.testview.canvas;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import com.nova.scenic.projectlibs.util.debug.MyLog;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static com.scenic.rd_android.testview.canvas.GLUtil.checkGlError;
import static com.scenic.rd_android.testview.canvas.GLUtil.createProgram;

/**
 * Created by scenic on 2017/5/6.
 */
class DebugGLRender implements GLSurfaceView.Renderer {

    private int mProgram;


    private final String mVertexShader =
            "uniform mat4 uMVPMatrix;\n" +
                    "attribute vec4 aPosition;\n" +
                    "void main() {\n" +
                    "  gl_Position = vec4(0.0 , 0.0 , 0.0 , 1.0);\n" +
                    "  gl_PointSize = 10.0; \n" +
                    "}\n";

    private final String mFragmentShader =
            "precision mediump float;\n" +
                    "void main() {\n" +
                    "  gl_FragColor = vec4(1,0,0,1);\n" +
                    "}\n";

    MyLog log = new MyLog("DebugGLRender");
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        log.debug("gl " + gl + " config " + config);
        mProgram = createProgram(mVertexShader, mFragmentShader);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        log.debug("gl " + gl + " width " + width + "   height " + height);

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClearColor(0f, 0f, 1f, 0.5f);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glUseProgram(mProgram);
        checkGlError("glUseProgram");
        GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
        checkGlError("drawPoint");
    }
}
